const sequelize = require('../config/database')
const { Product } = require('../models')

const sampleProducts = [
  {
    name: '专业彩色打印服务',
    description: '高质量彩色打印，适用于各类文档、图片、海报等。使用进口墨水，色彩鲜艳持久。支持多种纸张类型，满足不同需求。',
    original_price: 20.00,
    points_price: 100.00,
    image_url: '/static/icons/default-product.svg',
    category: 'print',
    type: 'color_print',
    paper_size: 'A4',
    paper_type: '铜版纸120g',
    color_type: 'color',
    stock_quantity: 999,
    stock_warning: 100,
    sales_count: 0,
    is_hot: true,
    is_new: false,
    sort_order: 100,
    status: 1
  },
  {
    name: '黑白文档打印',
    description: '经济实惠的黑白打印服务，适合日常文档、作业、报告等。清晰度高，成本低廉。',
    original_price: 10.00,
    points_price: 50.00,
    image_url: '/static/icons/default-product.svg',
    category: 'print',
    type: 'bw_print',
    paper_size: 'A4',
    paper_type: '普通纸80g',
    color_type: 'bw',
    stock_quantity: 999,
    stock_warning: 100,
    sales_count: 0,
    is_hot: false,
    is_new: false,
    sort_order: 90,
    status: 1
  },
  {
    name: '螺旋装订服务',
    description: '专业螺旋装订，可180度平摊，适合笔记本、日历、教材等装订。牢固耐用，美观实用。',
    original_price: 15.00,
    points_price: 80.00,
    image_url: '/static/icons/default-product.svg',
    category: 'service',
    type: 'binding',
    paper_size: null,
    paper_type: null,
    color_type: null,
    stock_quantity: 500,
    stock_warning: 50,
    sales_count: 0,
    is_hot: false,
    is_new: true,
    sort_order: 80,
    status: 1
  },
  {
    name: '高档铜版纸彩印',
    description: '使用进口铜版纸，适合高端宣传册、画册、菜单等印刷品。色彩饱和度高，质感优异。',
    original_price: 30.00,
    points_price: 150.00,
    image_url: '/static/icons/default-product.svg',
    category: 'print',
    type: 'color_print',
    paper_size: 'A4',
    paper_type: '铜版纸200g',
    color_type: 'color',
    stock_quantity: 300,
    stock_warning: 50,
    sales_count: 0,
    is_hot: true,
    is_new: true,
    sort_order: 95,
    status: 1
  },
  {
    name: 'A3大幅面打印',
    description: 'A3大幅面打印，适合海报、图纸、大型表格等。支持彩色和黑白两种模式。',
    original_price: 25.00,
    points_price: 120.00,
    image_url: '/static/icons/default-product.svg',
    category: 'print',
    type: 'color_print',
    paper_size: 'A3',
    paper_type: '普通纸100g',
    color_type: 'both',
    stock_quantity: 200,
    stock_warning: 30,
    sales_count: 0,
    is_hot: false,
    is_new: false,
    sort_order: 85,
    status: 1
  }
]

async function seedProducts() {
  try {
    console.log('开始创建示例商品...')
    
    // 确保数据库连接
    await sequelize.authenticate()
    console.log('数据库连接成功')
    
    // 清空现有商品（可选）
    // await Product.destroy({ where: {} })
    
    // 创建示例商品
    for (const productData of sampleProducts) {
      const existingProduct = await Product.findOne({ where: { name: productData.name } })
      if (!existingProduct) {
        await Product.create(productData)
        console.log(`创建商品: ${productData.name}`)
      } else {
        console.log(`商品已存在: ${productData.name}`)
      }
    }
    
    console.log('示例商品创建完成!')
    
    // 显示创建的商品
    const products = await Product.findAll()
    console.log(`\n总共有 ${products.length} 个商品:`)
    products.forEach(product => {
      console.log(`- ${product.name} (ID: ${product.id}, 积分: ${product.points_price}, 库存: ${product.stock_quantity})`)
    })
    
  } catch (error) {
    console.error('创建示例商品失败:', error)
    throw error
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  seedProducts().finally(() => {
    sequelize.close()
  })
}

module.exports = { seedProducts, sampleProducts }